package cn.sh.library.gmwx.sparql.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.ontoware.rdf2go.model.Model;
import org.ontoware.rdf2go.model.QueryResultTable;
import org.springframework.stereotype.Repository;

import cn.sh.library.gmwx.annotation.GraphDefine;
import cn.sh.library.gmwx.base.Constant;
import cn.sh.library.gmwx.bean.PersonSearchBean;
import cn.sh.library.gmwx.common.ModelManager;
import cn.sh.library.gmwx.dao.impl.BaseDaoImpl;
import cn.sh.library.gmwx.dto.Pager;
import cn.sh.library.gmwx.dto.QueryResult;
import cn.sh.library.gmwx.sparql.ItemSparql;
import cn.sh.library.gmwx.sparql.MergeParts;
import cn.sh.library.gmwx.sparql.PersonSparql;
import cn.sh.library.gmwx.sparql.RdfQuery;
import cn.sh.library.gmwx.sparql.SparqlExecution;
import cn.sh.library.gmwx.utils.RDFUtils;

@Repository
@GraphDefine(name = Constant.GRAPH_CPCPERSON)
public class PersonSparqlImpl extends BaseDaoImpl implements PersonSparql {
//    @Override
//    public List<Map<String, String>> getInstanceItems(String instance_uri) {
//        Model model_op = ModelManager.get(model);
//        QueryResultTable rst = model_op.sparqlSelect(RdfQuery.RDF_QUERY_INSTANCE_ITEMS.toString(instance_uri));
//        return RDFUtils.transformQueryResultTable(rst);
//    }
	
	public Map<String, Object> getCpcPersonInfoN(String uri){
		Map<String, Object> result = new HashMap<String, Object>();
		ArrayList list = SparqlExecution.vQuery(this.graph,
				RdfQuery.RDF_QUERY_PO4S_LITERAL.toString(uri), "p", "o");
		result = RDFUtils.getMap(list, "name", "");
		// result.put("officialEvent", getOfficialEventByPerson(uri));
		result.put("officialEvent", "");  // 列表页不取"任职履历"信息
		result.put("uri", uri);
		return result;
	}
	
	public Map<String, Object> getCpcPersonInfo(String uri){
		Map<String, Object> result = new HashMap<String, Object>();
		ArrayList list = SparqlExecution.vQuery(this.graph,
				RdfQuery.RDF_QUERY_PO4S_LITERAL.toString(uri), "p", "o");
		result = RDFUtils.getMap(list, "name", "");
		result.put("officialEvent", getOfficialEventByPerson(uri));
		result.put("uri", uri);
		return result;
	}
	
	public List<Map<String, Object>> getOfficialEventByPerson(String personUri) {
		//

		List list = RDFUtils.transformListMap(SparqlExecution.vQuery(this.graph, RdfQuery.RDF_QUERY_OFFICIALEVENT_BY_PERSON.toString(personUri), "eve", "relatedOrganization", "officialPosition", "identifier"));
		
		for(int i= 0; i< list.size(); i++) {
            String uri = ((Map) list.get(i)).get("eve").toString();
            ((Map) list.get(i)).put("temporal", getTemporalByEvent(uri));
		}
		
		return list;
//		return RDFUtils.transformListMap(SparqlExecution.vQuery(this.graph, RdfQuery.RDF_QUERY_OFFICIALEVENT_BY_PERSON.toString(personUri), "eve", "relatedOrganization", "officialPosition", "begin", "end", "identifier"));
	}
	
	public List<Map<String, String>> getTemporalByEvent(String eve){
		
		return RDFUtils.transformListMap(SparqlExecution.vQuery(this.graph, RdfQuery.RDF_QUERY_TEMPORAL_BY_EVENT.toString(eve), "begin", "end"));
	}
	
	@Override
	public QueryResult<Map<String, Object>> getCpcPersons(PersonSearchBean search, Pager pager, String order){
		QueryResult<Map<String, Object>> result = new QueryResult<Map<String,Object>>();
		String clause = MergeParts.personSearch(search);

        Long count = null;
        ArrayList results = new ArrayList();
        results = SparqlExecution.vQuery(graph, RdfQuery.RDF_COUNT_CPC_PERSONS.toString(clause), "count");
        count = Long.parseLong(RDFUtils.getValue(((Map) results.get(0)).get("count").toString()));
        result.setTotalrecord(count);
        String sort = " order by ASC(?enName) ";
        
        if(count > 0) {
        	results = SparqlExecution.vQuery(graph, RdfQuery.RDF_QUERY_CPC_PERSONS.toString(clause, sort, pager.getStartIndex(), pager.getPageSize()), "uri");
        	
        	for (int i=0; i<results.size(); i++) {
                String uri = ((Map) results.get(i)).get("uri").toString();
                ((Map) results.get(i)).putAll(getCpcPersonInfoN(uri));
        	}
        	result.setResultList(results);
        }
        
		return result;
	}
}
